﻿using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System;
using System.Threading;
using Timer = System.Windows.Forms.Timer;

/* Authors: Mike Poddo, Matt Gillette
 * Missile Command
 */
namespace MissileCommand
{
    class DefensiveMissileShots
    {
        private Graphics g;
        private PointF startPoint;
        private PointF endPoint;
        private Boolean active;
        private List<PointF> PointList = new List<PointF>();
        private int pointIndex;
        private Thread boomThread;
        private MissileCollisionBoom boom;
        private Timer defensiveMissileTimer;


        public DefensiveMissileShots(int xS, int yS, int xE, int yE, Graphics g)
        {
            startPoint = new Point(xS, yS);
            endPoint = new Point(xE, yE);
            this.g = g;
            this.g.SmoothingMode = SmoothingMode.AntiAlias;
            this.active = true;
            CalcPoints();
            //setup timer
            defensiveMissileTimer = new Timer();
            defensiveMissileTimer.Interval = 25;
            defensiveMissileTimer.Start();
            defensiveMissileTimer.Tick += new EventHandler(Draw);
            //setup boom/explosion
            boom = new MissileCollisionBoom(g, endPoint);
            boomThread = new Thread(new ThreadStart(boom.drawBoom));
        }


        private void Draw(object sender, System.EventArgs e)
        {
            if (pointIndex < PointList.Count & this.active)
            {
                for (int i = 0; i < pointIndex; i++)
                {
                    g.DrawLine(Pens.AliceBlue, startPoint.X, startPoint.Y, PointList[i + 1].X, PointList[i + 1].Y);
                }
                pointIndex++;
            }
            else if (this.active)// draw circle/boom
            {
                this.active = false;
                Pen myPen = new Pen(Color.Black);
                myPen.Width = 5;
                g.DrawLine(myPen, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
                defensiveMissileTimer.Stop();//stop timer
                boomThread.Start(); //draw boom

                defensiveMissileTimer.Dispose();
            }
        }


        public Boolean isActive()
        {
            if (!active)
            {
                //this.defensiveMissileTimer.Dispose();
                //this.g.Dispose();

                return active;
            }

            return active;
        }


        private void CalcPoints()
        {
            // Get Points From Line(s)
            float curDist = 0;
            float distance = 0;

            PointF ptA = startPoint;
            PointF ptB = endPoint;
            float deltaX = ptB.X - ptA.X;
            float deltaY = ptB.Y - ptA.Y;
            curDist = 0;
            distance = (float)Math.Sqrt(Math.Pow(deltaX, 2) + Math.Pow(deltaY, 2));
            while (curDist < distance)
            {
                curDist += 6; //adjust speed of line
                float offsetX = (float)((double)curDist / (double)distance * (double)deltaX);
                float offsetY = (float)((double)curDist / (double)distance * (double)deltaY);
                PointList.Add(new PointF(ptA.X + offsetX, ptA.Y + offsetY));
            }
            PointList.Add(new PointF(endPoint.X, endPoint.Y));
        }

        public void PauseMe()
        {
            defensiveMissileTimer.Stop();
        }

        public void UnPauseMe()
        {
            defensiveMissileTimer.Start();
        }


    }
}
